package tw.rescue.util;

import javax.servlet.http.HttpServletRequest;

public class JspUtil {

	public static void setContextPath(HttpServletRequest request) {
		String ctxPath = request.getContextPath();
		request.setAttribute("ctxPath", ctxPath);
	}

	public static void setLocalJQueryPath(HttpServletRequest request) {
		String jqueryPath = request.getContextPath() + "/js/jquery-1.3.2.min.js";
		request.setAttribute("jqueryPath", jqueryPath);
	}

	public static void setGoogleJQueryPath(HttpServletRequest request) {
		String s = "://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
		String jqueryPath = (request.isSecure() ? "https" : "http") + s;
		request.setAttribute("jqueryPath", jqueryPath);
	}

	public static void setLocalJQueryUiPath(HttpServletRequest request) {
		String jqueryUiJsPath = request.getContextPath() + "/js/jquery-ui-1.7.2.custom.min.js";
		request.setAttribute("jqueryUiJsPath", jqueryUiJsPath);
		String jqueryUiCssPath = request.getContextPath() + "/css/sunny/jquery-ui-1.7.2.custom.css";
		request.setAttribute("jqueryUiCssPath", jqueryUiCssPath);
	}

	public static void setGoogleJQueryUiPath(HttpServletRequest request) {
		String s = "://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js";
		String jqueryUiJsPath = (request.isSecure() ? "https" : "http") + s;
		request.setAttribute("jqueryUiJsPath", jqueryUiJsPath);
		String jqueryUiCssPath = request.getContextPath() + "/css/sunny/jquery-ui-1.7.2.custom.css";
		request.setAttribute("jqueryUiCssPath", jqueryUiCssPath);
	}

	public static void setIsBeforeIE7(HttpServletRequest request) {
		String agent = request.getHeader("User-Agent");
		if (agent==null) return;
		String ie = "MSIE";
		try {
			int idx = agent.indexOf(ie);
			if (idx<0) return;
			String s = agent.substring(idx+ie.length());
			idx = s.indexOf(".");
			s = s.substring(0, idx).trim();
			int version = Integer.parseInt(s);
			if (version<=6)
				request.setAttribute("isBeforeIE7", true);
		} catch (Exception e) {
		}
	}

	public static void setup(HttpServletRequest request) {
		request.setAttribute("isSecure", request.isSecure());
		JspUtil.setContextPath(request);
		JspUtil.setGoogleJQueryPath(request);
		JspUtil.setGoogleJQueryUiPath(request);
		JspUtil.setIsBeforeIE7(request);
	}
}
